package com.app.utils;

import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by lzy on 2021/9/9.
 */
public class TxtAdapter extends RecyclerView.Adapter<TxtViewHolder> {
    private List<SpannableString> list = new CopyOnWriteArrayList<>();

    public void appendTxt(SpannableString txt) {
        list.add(txt);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TxtViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_txt_layout, parent, false);
        return new TxtViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TxtViewHolder holder, int position) {
        holder.textView.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void clearAll() {
        list.clear();
        notifyDataSetChanged();
    }
}
